﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using Oscilloscope.Adjust;
using Oscilloscope.Channel;
using Oscilloscope.Screen;
using Oscilloscope.Util;

namespace Vosc.VirtualOscilloscope
{
    // Token: 0x02000004 RID: 4
    public class ChannelControl : UserControl
    {
        // Token: 0x06000012 RID: 18 RVA: 0x00003F2A File Offset: 0x0000212A
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        // Token: 0x06000013 RID: 19 RVA: 0x00003F4C File Offset: 0x0000214C
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.TbrValuePerCell = new System.Windows.Forms.TrackBar();
            this.LblSignalName = new System.Windows.Forms.Label();
            this.LblValuePerCell = new System.Windows.Forms.Label();
            this.LblVerPos = new System.Windows.Forms.Label();
            this.BtnColor = new System.Windows.Forms.Button();
            this.CbxLineWidth = new System.Windows.Forms.ComboBox();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.labelBitMask = new System.Windows.Forms.Label();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label_offs = new System.Windows.Forms.TextBox();
            this.textBox_Base = new System.Windows.Forms.TextBox();
            this.button_Move = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.TbrValuePerCell)).BeginInit();
            this.SuspendLayout();
            // 
            // TbrValuePerCell
            // 
            this.TbrValuePerCell.Location = new System.Drawing.Point(169, 0);
            this.TbrValuePerCell.Maximum = 20;
            this.TbrValuePerCell.Name = "TbrValuePerCell";
            this.TbrValuePerCell.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.TbrValuePerCell.Size = new System.Drawing.Size(186, 45);
            this.TbrValuePerCell.TabIndex = 1;
            this.TbrValuePerCell.TickStyle = System.Windows.Forms.TickStyle.Both;
            this.TbrValuePerCell.Scroll += new System.EventHandler(this.TbrValuePerCell_Scroll);
            // 
            // LblSignalName
            // 
            this.LblSignalName.AutoSize = true;
            this.LblSignalName.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.LblSignalName.Location = new System.Drawing.Point(191, 10);
            this.LblSignalName.Name = "LblSignalName";
            this.LblSignalName.Size = new System.Drawing.Size(82, 11);
            this.LblSignalName.TabIndex = 2;
            this.LblSignalName.Text = "channelname";
            this.LblSignalName.Visible = false;
            // 
            // LblValuePerCell
            // 
            this.LblValuePerCell.BackColor = System.Drawing.SystemColors.Control;
            this.LblValuePerCell.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.LblValuePerCell.Location = new System.Drawing.Point(355, 13);
            this.LblValuePerCell.Name = "LblValuePerCell";
            this.LblValuePerCell.Size = new System.Drawing.Size(37, 18);
            this.LblValuePerCell.TabIndex = 3;
            this.LblValuePerCell.Text = "1000";
            this.LblValuePerCell.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // LblVerPos
            // 
            this.LblVerPos.BackColor = System.Drawing.SystemColors.Control;
            this.LblVerPos.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.LblVerPos.Location = new System.Drawing.Point(355, 31);
            this.LblVerPos.Name = "LblVerPos";
            this.LblVerPos.Size = new System.Drawing.Size(55, 18);
            this.LblVerPos.TabIndex = 5;
            this.LblVerPos.Text = "1000";
            this.LblVerPos.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.LblVerPos.Visible = false;
            // 
            // BtnColor
            // 
            this.BtnColor.BackColor = System.Drawing.Color.Fuchsia;
            this.BtnColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.BtnColor.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.BtnColor.Location = new System.Drawing.Point(86, 2);
            this.BtnColor.Margin = new System.Windows.Forms.Padding(0);
            this.BtnColor.Name = "BtnColor";
            this.BtnColor.Size = new System.Drawing.Size(30, 40);
            this.BtnColor.TabIndex = 7;
            this.BtnColor.Text = "16";
            this.BtnColor.UseVisualStyleBackColor = false;
            this.BtnColor.Click += new System.EventHandler(this.BtnColor_Click);
            // 
            // CbxLineWidth
            // 
            this.CbxLineWidth.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.CbxLineWidth.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.CbxLineWidth.FormattingEnabled = true;
            this.CbxLineWidth.Location = new System.Drawing.Point(287, 106);
            this.CbxLineWidth.Name = "CbxLineWidth";
            this.CbxLineWidth.Size = new System.Drawing.Size(39, 20);
            this.CbxLineWidth.TabIndex = 8;
            // 
            // labelBitMask
            // 
            this.labelBitMask.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.labelBitMask.Location = new System.Drawing.Point(248, 106);
            this.labelBitMask.Name = "labelBitMask";
            this.labelBitMask.Size = new System.Drawing.Size(33, 20);
            this.labelBitMask.TabIndex = 9;
            this.labelBitMask.Text = "0000";
            this.labelBitMask.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.Location = new System.Drawing.Point(119, 1);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(44, 43);
            this.flowLayoutPanel1.TabIndex = 1;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(244, 144);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(60, 22);
            this.button1.TabIndex = 10;
            this.button1.Text = "FFFF";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.labelBitMask_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(246, 129);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(59, 12);
            this.label1.TabIndex = 11;
            this.label1.Text = "数字信号:";
            // 
            // label_offs
            // 
            this.label_offs.Location = new System.Drawing.Point(407, 51);
            this.label_offs.Name = "label_offs";
            this.label_offs.Size = new System.Drawing.Size(60, 21);
            this.label_offs.TabIndex = 15;
            this.label_offs.TextChanged += new System.EventHandler(this.label_offs_TextChanged);
            // 
            // textBox_Base
            // 
            this.textBox_Base.Location = new System.Drawing.Point(5, 25);
            this.textBox_Base.Name = "textBox_Base";
            this.textBox_Base.Size = new System.Drawing.Size(75, 21);
            this.textBox_Base.TabIndex = 17;
            this.textBox_Base.TextChanged += new System.EventHandler(this.textBox_Base_TextChanged);
            // 
            // button_Move
            // 
            this.button_Move.Location = new System.Drawing.Point(5, -1);
            this.button_Move.Name = "button_Move";
            this.button_Move.Size = new System.Drawing.Size(75, 23);
            this.button_Move.TabIndex = 18;
            this.button_Move.Text = "移至零点";
            this.button_Move.UseVisualStyleBackColor = true;
            this.button_Move.Click += new System.EventHandler(this.button_move_Click);
            // 
            // ChannelControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.Control;
            this.Controls.Add(this.button_Move);
            this.Controls.Add(this.textBox_Base);
            this.Controls.Add(this.label_offs);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.LblValuePerCell);
            this.Controls.Add(this.LblVerPos);
            this.Controls.Add(this.TbrValuePerCell);
            this.Controls.Add(this.flowLayoutPanel1);
            this.Controls.Add(this.labelBitMask);
            this.Controls.Add(this.CbxLineWidth);
            this.Controls.Add(this.BtnColor);
            this.Controls.Add(this.LblSignalName);
            this.Name = "ChannelControl";
            this.Size = new System.Drawing.Size(475, 50);
            ((System.ComponentModel.ISupportInitialize)(this.TbrValuePerCell)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        // Token: 0x06000014 RID: 20 RVA: 0x00004469 File Offset: 0x00002669
        static ChannelControl()
        {
            ChannelControl.lineWidthComboIndexMap.Add(2f, 0);
            ChannelControl.lineWidthComboIndexMap.Add(3f, 1);
            ChannelControl.lineWidthComboIndexMap.Add(4f, 2);
        }

        private Label label1;
        private TextBox label_offs;
        private TextBox textBox_Base;
        private Button button_Move;
        private Button button1;

        // Token: 0x14000001 RID: 1
        // (add) Token: 0x06000015 RID: 21 RVA: 0x000044A5 File Offset: 0x000026A5
        // (remove) Token: 0x06000016 RID: 22 RVA: 0x000044BE File Offset: 0x000026BE
        public event ChannelPropertyChangedEventHandler ChannelPropertyChanged;

        // Token: 0x17000002 RID: 2
        // (get) Token: 0x06000017 RID: 23 RVA: 0x000044D7 File Offset: 0x000026D7
        public int ChannelSN
        {
            get { return this.channelSN; }
        }

        // Token: 0x17000003 RID: 3
        // (get) Token: 0x06000018 RID: 24 RVA: 0x000044DF File Offset: 0x000026DF
        public Color LineColor
        {
            get { return this.lineColor; }
        }

        // Token: 0x17000004 RID: 4
        // (get) Token: 0x06000019 RID: 25 RVA: 0x000044E7 File Offset: 0x000026E7
        public float LineWidth
        {
            get { return this.lineWidth; }
        }

        // Token: 0x17000005 RID: 5
        // (set) Token: 0x0600001A RID: 26 RVA: 0x000044EF File Offset: 0x000026EF
        public string SignalName
        {
            set { this.signalName = value; }
        }

        // Token: 0x17000006 RID: 6
        // (get) Token: 0x0600001B RID: 27 RVA: 0x000044F8 File Offset: 0x000026F8
        public int ValuePerCellGrade
        {
            get { return this.valuePerCellGrade; }
        }

        // Token: 0x17000007 RID: 7
        // (get) Token: 0x0600001C RID: 28 RVA: 0x00004500 File Offset: 0x00002700
        public ushort BitMask
        {
            get { return this.bitMask; }
        }

        // Token: 0x17000008 RID: 8
        // (get) Token: 0x0600001D RID: 29 RVA: 0x00004508 File Offset: 0x00002708
        // (set) Token: 0x0600001E RID: 30 RVA: 0x00004510 File Offset: 0x00002710
        public double VerPos
        {
            get { return this.verPos; }
            set { this.SetVerPos(value);
                textBox_Base.Text = value.ToString();
            }
        }

        // Token: 0x0600001F RID: 31 RVA: 0x00004519 File Offset: 0x00002719
        public ChannelControl()
            : this(
                Consts.DEFAULT_CHANNEL_SN,
                Consts.DEFAULT_WAVE_LINE_COLOR,
                Consts.DEFAULT_WAVE_LINE_WIDTH,
                Consts.DEFAULT_SIAGNAL_NAME,
                Consts.DEFAULT_VALUE_PER_CELL_GRADE,
                Consts.DEFAULT_VERTICAL_POSITION,
                Consts.DEFAULT_BIT_MASK,
                Consts.DEFAULT_OffSEt,
                Consts.DEFAULT_BaseLine
            ) { }

        List<ChannelControl> channelControls = new List<ChannelControl>();

        // Token: 0x06000020 RID: 32 RVA: 0x00004544 File Offset: 0x00002744
        public ChannelControl(
            int channelSN,
            Color lineColor,
            float lineWidth,
            string signalName,
            int valuePerCellGrade,
            double verPos,
            ushort bitMask,
            double off,
            double Bseline
        )
        {
            this.InitializeComponent();
            this.TbrValuePerCell.Maximum = ValuePerCellAdjustGrade.Instance.GradeCount - 1;
            foreach (float num in ChannelControl.lineWidthComboIndexMap.Keys)
            {
                float num2 = num;
                this.CbxLineWidth.Items.Add(num2.ToString());
            }
            this.channelSN = channelSN;
            this.lineColor = lineColor;
            this.lineWidth = lineWidth;
            this.offset = 0;
            this.signalName = signalName;
            this.valuePerCellGrade = valuePerCellGrade;
            this.verPos = verPos;
            this.bitMask = bitMask;
            this.BaseLine = Bseline;
            this.toolTip.SetToolTip(this.BtnColor, "颜色选择");
            this.toolTip.SetToolTip(this.CbxLineWidth, "波形线宽选择");
            this.toolTip.SetToolTip(this.TbrValuePerCell, "值/格 调整");
            this.toolTip.SetToolTip(this.LblValuePerCell, "当前 值/格");
            this.toolTip.SetToolTip(this.LblVerPos, "当前垂直位置");
            this.toolTip.SetToolTip(this.labelBitMask, "复合数字信号位选择");
            this.Load += ChannelControl_Load;
        }

        public void GetControl(Control control)
        {
            flowLayoutPanel1.Controls.Add(control);
        }

        public Color GetColor()
        {
            return lineColor;
        }

        // Token: 0x06000021 RID: 33 RVA: 0x0000468C File Offset: 0x0000288C
        private void SetChannelSN(int channelSN)
        {
            this.channelSN = channelSN;
            this.BtnColor.Text = channelSN.ToString();
        }

        // Token: 0x06000022 RID: 34 RVA: 0x000046A7 File Offset: 0x000028A7
        private void SetLineColor(Color color, bool fromUI)
        {
            this.lineColor = color;
            this.BtnColor.BackColor = color;
            if (fromUI)
            {
                this.NotifyPropertyChanged(ChannelProperty.ChannelLineColor, color);
            }
        }

        // Token: 0x06000023 RID: 35 RVA: 0x000046CC File Offset: 0x000028CC
        private void SetLineWidth(float lineWidth, bool fromUI)
        {
            this.lineWidth = lineWidth;
            if (fromUI)
            {
                this.NotifyPropertyChanged(ChannelProperty.ChannelLineWidth, lineWidth);
                return;
            }
            this.CbxLineWidth.SelectedIndex = ChannelControl.lineWidthComboIndexMap[lineWidth];
        }

        // Token: 0x06000024 RID: 36 RVA: 0x000046FC File Offset: 0x000028FC
        private void SetSignalName(string signalName)
        {
            this.signalName = signalName;
            this.LblSignalName.Text = signalName;
        }

        // Token: 0x06000025 RID: 37 RVA: 0x00004711 File Offset: 0x00002911
        private void SetValuePerCellGrade(int valuePerCellGrade, bool fromUI)
        {
            this.valuePerCellGrade = valuePerCellGrade;
            this.LblValuePerCell.Text = ValuePerCellAdjustGrade.Instance.GetDisplayStrOfGrade(
                valuePerCellGrade
            );
            if (fromUI)
            {
                this.NotifyPropertyChanged(ChannelProperty.ChannelValuePerCell, valuePerCellGrade);
                return;
            }
            this.TbrValuePerCell.Value = valuePerCellGrade;
        }

        // Token: 0x06000026 RID: 38 RVA: 0x0000474D File Offset: 0x0000294D
        private void SetBitMask(ushort bitMask, bool fromUI)
        {
            this.bitMask = bitMask;
            this.button1.Text = bitMask.ToString("X4");
            if (fromUI)
            {
                this.NotifyPropertyChanged(ChannelProperty.ChannelBitMask, bitMask);
            }
        }
        private void SetOffset(double offset, bool fromUI)
        {
            this.offset = offset;
            this.offset = 0;
            this.label_offs.Text = offset.ToString();
            if (fromUI)
            {
                this.NotifyPropertyChanged(ChannelProperty.ChannelOfset, offset);
            }
        }
        private void SetBseLine(double offset, bool fromUI)
        {
            this.BaseLine = BaseLine;
            this.textBox_Base.Text = offset.ToString();
            if (fromUI)
            {
                this.NotifyPropertyChanged(ChannelProperty.ChannelBitMask, offset);
            }
        }

        // Token: 0x06000027 RID: 39 RVA: 0x0000477D File Offset: 0x0000297D
        private void SetVerPos(double verPos)
        {
            this.verPos = verPos;
            this.LblVerPos.Text = InternalUtils.GetDisplayStrOfValue(verPos);
        }

        // Token: 0x06000028 RID: 40 RVA: 0x00004798 File Offset: 0x00002998
        private void BtnColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.SetLineColor(colorDialog.Color, true);
            }
        }

        // Token: 0x06000029 RID: 41 RVA: 0x000047C2 File Offset: 0x000029C2
        private void NotifyPropertyChanged(ChannelProperty changedProperty, object newValue)
        {
            if (this.ChannelPropertyChanged != null)
            {
                this.ChannelPropertyChanged(
                    this.ChannelSN,
                    new ChannelPropertyChangedEventArgs(changedProperty, newValue)
                );
            }
        }

        // Token: 0x0600002A RID: 42 RVA: 0x000047E4 File Offset: 0x000029E4
        private void TbrValuePerCell_Scroll(object sender, EventArgs e)
        {
            this.SetValuePerCellGrade(((TrackBar)sender).Value, true);
        }

        // Token: 0x0600002B RID: 43 RVA: 0x000047F8 File Offset: 0x000029F8
        private void ChannelControl_Load(object sender, EventArgs e)
        {
            this.SetChannelSN(this.channelSN);
            this.SetLineColor(this.lineColor, false);
            this.SetLineWidth(this.lineWidth, false);
            this.SetSignalName(this.signalName);
            this.SetValuePerCellGrade(this.valuePerCellGrade, false);
            this.SetVerPos(this.verPos);
            this.SetBitMask(this.bitMask, false);
            this.SetOffset(this.offset, false);
            this.SetBseLine(this.BaseLine, false);
            this.CbxLineWidth.SelectedIndexChanged += this.CbxLineWidth_SelectedIndexChanged;
        }

        /// <summary>
        /// 加载配置
        /// </summary>
        public void LoadChannelControlConfig(
            int channelSN,
            Color lineColor,
            float lineWidth,
            string signalName,
            int valuePerCellGrade,
            double verPos,
            ushort bitMask
        )
        {
            SetChannelSN(channelSN);
            SetLineColor(lineColor, false);
            SetLineWidth(lineWidth, false);
            SetSignalName(signalName);
            SetValuePerCellGrade(valuePerCellGrade, false);
            SetVerPos(verPos);
            SetBitMask(bitMask, false);
            SetOffset(offset, false);
        }

        // Token: 0x0600002C RID: 44 RVA: 0x00004874 File Offset: 0x00002A74
        private void CbxLineWidth_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetLineWidth(float.Parse(((ComboBox)sender).SelectedItem.ToString()), true);
        }

        // Token: 0x0600002D RID: 45 RVA: 0x00004894 File Offset: 0x00002A94
        private void labelBitMask_Click(object sender, EventArgs e)
        {
            BitMaskForm bitMaskForm = new BitMaskForm(this.bitMask);
            if (bitMaskForm.ShowDialog() == DialogResult.OK)
            {
                this.SetBitMask(bitMaskForm.BitMask, true);
            }
        }

        // Token: 0x04000030 RID: 48
        private IContainer components;

        // Token: 0x04000031 RID: 49
        private TrackBar TbrValuePerCell;

        // Token: 0x04000032 RID: 50
        private Label LblSignalName;

        // Token: 0x04000033 RID: 51
        private Label LblValuePerCell;

        // Token: 0x04000034 RID: 52
        private Label LblVerPos;

        // Token: 0x04000035 RID: 53
        private Button BtnColor;

        // Token: 0x04000036 RID: 54
        private ComboBox CbxLineWidth;

        // Token: 0x04000037 RID: 55
        private ToolTip toolTip;

        // Token: 0x04000038 RID: 56
        private Label labelBitMask;

        // Token: 0x04000039 RID: 57
        private static Dictionary<float, int> lineWidthComboIndexMap = new Dictionary<float, int>();

        // Token: 0x0400003B RID: 59
        public int channelSN;

        // Token: 0x0400003C RID: 60
        public Color lineColor;

        // Token: 0x0400003D RID: 61
        public float lineWidth;

        // Token: 0x0400003E RID: 62
        public string signalName;

        // Token: 0x0400003F RID: 63
        public int valuePerCellGrade;
        public double offset;

        // Token: 0x04000040 RID: 64
        public ushort bitMask;
        public double BaseLine;
        private FlowLayoutPanel flowLayoutPanel1;

        // Token: 0x04000041 RID: 65
        public double verPos;

        /// <summary>
        /// 监测当前参数地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e) { }

        private void button_move_Click(object sender, EventArgs e)
        {
            try
            {
                OscilloscopeScreen.leftDrawAreaDrwaVPosArrow?.Invoke(channelSN);
            }
            catch (Exception) { }
        }

        private void label_offs_TextChanged(object sender, EventArgs e)
        {
            if (double.TryParse(label_offs.Text, out double value))
            {
                this.SetOffset(value, true);
            }
            else
            {
                // 如果转换失败，直接赋值为 0
                value = 0;
                SystemSetting.Channels[channelSN].signalOffset = value; //获取当前通道的偏移量
                this.SetOffset(value, true);
            }
        }

        private void textBox_Base_TextChanged(object sender, EventArgs e)
        {
            if (double.TryParse(textBox_Base.Text, out double value))
            {
                try
                {
                    OscilloscopeScreen.leftDrawAreaDrwaVPosArrowWithDouble?.Invoke(channelSN, value);
                }
                catch (Exception) { }
            }
            else
            {
                //// 如果转换失败，直接赋值为 0
                //value = 0;
                //try
                //{
                //    OscilloscopeScreen.leftDrawAreaDrwaVPosArrowWithDouble?.Invoke(channelSN, value);
                //}
                //catch (Exception) { }
            }
        }
    }
}
